//
//  BSKImagePikerController.m
//  MicroMemory
//
//  Created by rimi on 16/7/15.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKImagePikerController.h"
#import "BSKconsts.h"

@interface BSKImagePikerController ()
@property(nonatomic,strong) UIImageView * imageview;
@property(nonatomic,strong) UIView * borderView;
@property(nonatomic,strong) CALayer * maskLayer;
@property(nonatomic,strong) void(^ backBlock)(UIImage *);
@property(nonatomic,assign) CGSize originalSize;
@end


@implementation BSKImagePikerController

- (instancetype)initWithImage:(UIImage *)image toSize:(CGSize)toSize backBlock:(void (^)(UIImage *))backBlock
{
    self = [super init];
    if (self) {
        _toSize = toSize;
        _image = image;
        _backBlock = backBlock;
        _originalSize = image.size;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    _borderView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-_toSize.width)/2, (ScreenHeight-_toSize.height)/2, _toSize.width,_toSize.height)];
    _borderView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_borderView];
    _imageview = [[UIImageView alloc]initWithImage:_image];
    _imageview.center =CGPointMake(_toSize.width/2, _toSize.height/2);
    //	_imageview.userInteractionEnabled = YES;
    //缩放手势
    UIPinchGestureRecognizer * pinchgesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(PinchAction:)];
    [self.view addGestureRecognizer:pinchgesture];
    
    //拖动手势
    
    UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:panGesture];
    
    [_borderView addSubview:_imageview];
    _maskLayer = [[CALayer alloc]init];
    _maskLayer.frame = self.view.frame;
    CALayer * layer1 = [[CALayer alloc]init];
    layer1.frame = CGRectMake(0, 0, ScreenWidth, (ScreenHeight-_toSize.height)/2);
    layer1.backgroundColor = [UIColor blackColor].CGColor;
    layer1.opacity = 0.4;
    
    CALayer * layer2 = [[CALayer alloc]init];
    layer2.frame = CGRectMake(0, (ScreenHeight-_toSize.height)/2, (ScreenWidth-_toSize.width)/2, _toSize.height);
    layer2.backgroundColor = [UIColor blackColor].CGColor;
    layer2.opacity = 0.4;
    
    CALayer * layer3 = [[CALayer alloc]init];
    layer3.frame = CGRectMake((ScreenWidth-_toSize.width)/2+_toSize.width, (ScreenHeight-_toSize.height)/2, (ScreenWidth-_toSize.width)/2, _toSize.height);
    layer3.backgroundColor = [UIColor blackColor].CGColor;
    layer3.opacity = 0.4;
    
    CALayer * layer4 = [[CALayer alloc]init];
    layer4.frame = CGRectMake(0, (ScreenHeight-_toSize.height)/2+_toSize.height, ScreenWidth, (ScreenHeight-_toSize.height)/2);
    layer4.backgroundColor = [UIColor blackColor].CGColor;
    layer4.opacity = 0.4;
    
    CALayer * layer5 = [[CALayer alloc]init];
    layer5.frame = CGRectMake((ScreenWidth-_toSize.width)/2, (ScreenHeight-_toSize.height)/2, _toSize.width,_toSize.height);
    layer5.backgroundColor = [UIColor blackColor].CGColor;
    layer5.opacity = 1;
    
    [_maskLayer addSublayer:layer1];
    [_maskLayer addSublayer:layer2];
    [_maskLayer addSublayer:layer3];
    [_maskLayer addSublayer:layer4];
    [_maskLayer addSublayer:layer5];
    //	self.view.layer.mask = _maskLayer;
    UIButton * OKButton = [UIButton buttonWithType:UIButtonTypeSystem];
    OKButton.frame = CGRectMake(0, ScreenHeight - 30, ScreenWidth/2, 30);
    [OKButton setTitle:@"确定" forState:UIControlStateNormal];
    OKButton.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.395755043512658];
    [OKButton addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    OKButton.tag = 2000;
    [OKButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIButton * cancellButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancellButton.frame = CGRectMake( ScreenWidth/2, ScreenHeight - 30, ScreenWidth/2, 30);
    [cancellButton setTitle:@"取消" forState:UIControlStateNormal];
    cancellButton.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.395755043512658];
    [cancellButton addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cancellButton.tag = 2001;
    [cancellButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.view addSubview:OKButton];
    [self.view addSubview:cancellButton];
}

-(void)viewDidAppear:(BOOL)animated{
    self.view.layer.mask = _maskLayer;
}

-(void)ButtonClick:(UIButton *)sender{
    if (sender.tag == 2000) {
        if (_backBlock!=nil) {
            _backBlock([self snapshot]);
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (UIImage *)snapshot
{
    UIGraphicsBeginImageContextWithOptions(_borderView.bounds.size, YES, 0);
    [_borderView drawViewHierarchyInRect:_borderView.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}



-(void)panAction:(UIPanGestureRecognizer * )sender{
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            break;
            
        case UIGestureRecognizerStateChanged:{
            //取得手势在指定视图改变的坐标
            CGPoint ttransition = [sender translationInView:self.view];
            //更新animationView的坐标
            _imageview.center = CGPointMake(_imageview.center.x+ttransition.x, _imageview.center.y+ttransition.y);
            //重置增量
            [sender setTranslation:CGPointMake(0, 0) inView:self.view];
        }
            break;
        case UIGestureRecognizerStateEnded:
            [self CheckImage];
            break;
        default:
            break;
    }
}
-(void)PinchAction:(UIPinchGestureRecognizer * )sender{
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            break;
            
        case UIGestureRecognizerStateChanged:{
            _imageview.transform = CGAffineTransformScale(_imageview.transform, sender.scale, sender.scale);
            sender.scale = 1.0;
        }
            break;
        case UIGestureRecognizerStateEnded:
            [self CheckImage];
            break;
        default:
            break;
    }
}

-(void)CheckImage{
    
//    if (_imageview.accessibilityFrame.size.width<_toSize.width) {
//        
//        CGFloat scale = _toSize.width/_originalSize.width;
//		NSLog(@"scaleWidth:%lf  ,_tosize.width:%lf  ,_originalSize.width:%lf",scale,_toSize.width,_imageview.accessibilityFrame.size.width);
//        [UIView animateWithDuration:0.25 animations:^{
//            _imageview.transform = CGAffineTransformMakeScale(scale, scale);
//        }];
//    }
//    if (_imageview.accessibilityFrame.size.height<_toSize.height) {
//        
//		CGFloat scale = _toSize.height/_originalSize.height;
//		NSLog(@"scaleHeight:%lf  ,_tosize.height:%lf  ,_originalSize.height:%lf",scale,_toSize.height,_imageview.accessibilityFrame.size.height);
//        [UIView animateWithDuration:0.25 animations:^{
//            _imageview.transform = CGAffineTransformMakeScale(scale, scale);
//        }];
//    }
//    if (_imageview.frame.origin.x>0) {
//        CGRect tempFrame = _imageview.frame;
//        tempFrame.origin.x = 0;
//        [UIView animateWithDuration:0.25 animations:^{
//            _imageview.frame = tempFrame;
//        }];
//    }
//    if (_imageview.frame.origin.y>0) {
//        CGRect tempFrame = _imageview.frame;
//        tempFrame.origin.y = 0;
//        [UIView animateWithDuration:0.25 animations:^{
//            _imageview.frame = tempFrame;
//        }];
//    }
//    if (_imageview.frame.origin.x < (_imageview.accessibilityFrame.size.width - _toSize.width)*-1) {
//        CGRect tempFrame = _imageview.frame;
//        tempFrame.origin.x = (_imageview.accessibilityFrame.size.width - _toSize.width)*-1;
//        [UIView animateWithDuration:0.25 animations:^{
//            _imageview.frame = tempFrame;
//        }];
//    }
//    if (_imageview.frame.origin.y < (_imageview.accessibilityFrame.size.height - _toSize.height)*-1) {
//        CGRect tempFrame = _imageview.frame;
//        tempFrame.origin.y = (_imageview.accessibilityFrame.size.height - _toSize.height)*-1;
//        [UIView animateWithDuration:0.25 animations:^{
//            _imageview.frame = tempFrame;
//        }];
//    }
}

@end
